<?php
/**
 * 员工列表
 *
 * @author Dracowyn
 * @since 2023-11-15 16:19
 */

// 引入配置文件
include_once('config/config.php');
$loginAuth = check_auth();

$page = $_GET['page'] ?? 1;

$limit = 10;

$countSql = "SELECT COUNT(`p`.`id`) AS count FROM `pre_person` AS `p` LEFT JOIN `pre_department` AS `d` ON `p`.`depid` = `d`.`id` LEFT JOIN `pre_job` AS `j` ON `p`.`jobid` = `j`.`id` LEFT JOIN `pre_region` AS `pr` ON `p`.`province` = `pr`.`code` LEFT JOIN `pre_region` AS `c` ON `p`.`city` = `c`.`code` LEFT JOIN `pre_region` AS `dis` ON `p`.`district` = `dis`.`code`";
$count = find($countSql);

$offset = ($page - 1) * $limit;
$personSql = "SELECT `p`.*,`d`.`name` AS `dname`,`j`.`name` AS `jname`,`pr`.`name` AS `prname`,`c`.`name` AS `cname`,`dis`.`name` AS `disname` FROM `pre_person` AS `p` LEFT JOIN `pre_department` AS `d` ON `p`.`depid` = `d`.`id` LEFT JOIN `pre_job` AS `j` ON `p`.`jobid` = `j`.`id` LEFT JOIN `pre_region` AS `pr` ON `p`.`province` = `pr`.`code` LEFT JOIN `pre_region` AS `c` ON `p`.`city` = `c`.`code` LEFT JOIN `pre_region` AS `dis` ON `p`.`district` = `dis`.`code` ORDER BY `p`.`create_time` DESC LIMIT $offset,$limit";
$personData = all($personSql);
$showPage = showPage($page, $count['count'], $limit);

?>

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>员工管理</title>
	<?php include_once('common/meta.php'); ?>
</head>

<body>
<?php include_once('common/header.php'); ?>
<?php include_once('common/menu.php'); ?>
<div class="content">
    <div class="header">
        <h1 class="page-title">员工列表</h1>
    </div>
    <ul class="breadcrumb">
        <li><a href="index.php">首页</a> <span class="divider">/</span></li>
        <li class="active">员工管理</li>
    </ul>

    <div class="container-fluid">
        <div class="row-fluid">
            <div class="btn-toolbar">
                <button class="btn btn-primary" onClick="location='personAdd.php'"><i class="icon-plus"></i>添加员工
                </button>
            </div>
            <div class="well">
                <table class="table">
                    <thead>
                    <tr>
                        <th>ID</th>
                        <th>头像</th>
                        <th>员工姓名</th>
                        <th>手机号</th>
                        <th>性别</th>
                        <th>邮箱</th>
                        <!-- <th>地址</th> -->
                        <th>职位</th>
                        <th>部门</th>
                        <th>添加时间</th>
                        <th style="width: 26px;"></th>
                    </tr>
                    </thead>
                    <tbody>
					<?php foreach ($personData as $item) { ?>
                        <tr>
                            <td><?php echo $item['id']; ?></td>
                            <td>
								<?php if ($item['avatar'] != 'Array') { ?>
                                    <a href="<?php echo $item['avatar']; ?>" target="__blank">
                                        <img src="<?php echo 'upload/' . $item['avatar']; ?>" alt="" width="50"
                                             height="50">
                                    </a>
								<?php } else { ?>
                                    <img src="assets/images/upload.png" alt="" width="50" height="50">
								<?php } ?>
                            </td>
                            <td><?php echo $item['name']; ?></td>
                            <td><?php echo $item['mobile']; ?></td>
                            <td><?php echo $item['gender'] == 1 ? '男' : ($item['gender'] == 2 ? '女' : '保密'); ?></td>
                            <td><?php echo $item['email']; ?></td>
                            <!-- <td><?php // echo $item['prname'] . $item['cname'] . $item['disname'] . $item['address']; ?></td> -->
                            <td><?php echo $item['jname']; ?></td>
                            <td><?php echo $item['dname']; ?></td>
                            <td><?php echo date('Y-m-d H:i', $item['create_time']); ?></td>
                            <td>
                                <a href="/personEdit.php?id=<?php echo $item['id']; ?>"><i class="icon-pencil"></i></a>
                                <a href="#myModal" role="button" data-toggle="modal"
                                   data-id="<?php echo $item['id'] ?>"><i class="icon-remove"></i></a>
                            </td>
                        </tr>
					<?php } ?>
                    </tbody>
                </table>
            </div>
            <div class="pagination">
				<?php echo $showPage; ?>
            </div>

            <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
                 aria-hidden="true">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h3 id="myModalLabel">删除员工</h3>
                </div>
                <div class="modal-body">
                    <p class="error-text"><i class="icon-warning-sign modal-icon"></i>确认删除该员工？</p>
                </div>
                <div class="modal-footer">
                    <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
                    <button class="btn btn-danger" data-dismiss="modal">删除</button>
                </div>
            </div>
			<?php include_once('common/footer.php'); ?>
        </div>
    </div>
</div>
</body>
<?php echo include_once('common/script.php'); ?>
<script>
    let id = 0;
    $('a[href="#myModal"]').click(function () {
        id = $(this).data('id');
    });

    $('.btn-danger').click(function () {
        $.ajax({
            type: 'post',
            url: 'ajax.php',
            data: {
                action: 'del',
                table: 'person',
                id,
                adminId: '<?php echo $loginAuth['id']; ?>'
            },
            dataType: 'json',
            success: function (res) {
                if (res) {
                    if (res.code === 1) {
                        alert(res.msg)
                        location.reload();
                    } else {
                        alert(res.msg);
                    }
                }
            }
        })
    })
</script>
</html>
